package com.scalar.forecast.projectSetup.actionCommands;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.scalar.forecast.projectSetup.forms.ProjectSetupForm;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.dao.UserDefinedFieldsDAO;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.salesPersonSetup.dao.SalesPersonSetupDAO;
import com.scalar.chiptrack.sales.dao.CustomerDAO;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * Processes the request to view the Project Setup Details
 *
 * Created by Veeru. 
 * Date: Feb 18, 2005
 * Time: 9:27:12 PM 
 */
public class ProjectSetupViewCommand extends ActionCommand{

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "viewProjectSetup" );

        ProjectSetupForm projectSetupForm = ( ProjectSetupForm )form;
        ProjectSetup projectSetup = projectSetupForm.getProjectSetup();
        int projectId = 0;
        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        String purpose = request.getParameter("purpose");
        if ( purpose != null ) {
            request.setAttribute( "purpose", request.getParameter("purpose").trim() );
        } else {
            request.setAttribute( "purpose","view" );
        }
        if ( request.getParameter("projectID") != null ) {
            try {
                projectId = Integer.parseInt( request.getParameter("projectID").trim() );
                projectSetup.setProjectID( projectId );

                ProjectSetupDAO.loadProjectSetupDetails( null, projectSetup );
                projectSetup.setCustomerName( CustomerDAO.getCustomerName( null, projectSetup.getCustomerID() ) );
                projectSetup.setOdmName( CustomerDAO.getCustomerName( null, projectSetup.getOdmID() ) );
                projectSetup.setEndUserName( CustomerDAO.getCustomerName( null, projectSetup.getEndUserId() ) );
                projectSetup.setSalesPerson( SalesPersonSetupDAO.getSalesPersonName( null, projectSetup.getSalesPersonID() ) );

                request.setAttribute( "selectedChipsList" , ProjectSetupDAO.loadSelectedProducts( null, projectId, "CHIP" ) );
                request.setAttribute( "selectedBoardsList" , ProjectSetupDAO.loadSelectedProducts( null, projectId, "BOARD" ) );
                request.setAttribute( "selectedKitsList" ,  ProjectSetupDAO.loadSelectedProducts( null, projectId, "KIT" ) );

                ArrayList userDefinedfields = UserDefinedFieldsDAO.loadUserDefinedFieldValues( null, projectId, "PROJSETUP" );
                projectSetup.setUserDefinedFeilds( userDefinedfields );
                if ( "delete".equalsIgnoreCase( purpose ) )
                {
                    if ( ProjectSetupDAO.isProjectHasForecastEntry ( null, projectId ) )
                    {
                        request.setAttribute ( "isForecastEntered", "true" );
                    }
                }

            } catch ( Exception e ) {
                e.printStackTrace();
                errors.put( "Error while loading the Project Setup details", null );
                actionForward = mapping.findForward( "projectSearch" );
            }
        }
        return actionForward;
    }

}
